package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.IotAlarmTriggerRpcService;
import com.hunttown.mes.rpc.domain.IotAlarmTriggerDTO;
import com.hunttown.mes.rpc.domain.query.IotAlarmTriggerDTOQuery;
import com.hunttown.mes.service.IotAlarmTriggerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.IotAlarmTriggerConverts.convert;

/**
 * created by wangjunfu on 2021-03-04 19:17:10
 */
@Service
public class IotAlarmTriggerRpcServiceImpl implements IotAlarmTriggerRpcService {

    private final Logger logger = LoggerFactory.getLogger(IotAlarmTriggerRpcServiceImpl.class);

    private final IotAlarmTriggerService iotAlarmTriggerService;

    @Autowired
    public IotAlarmTriggerRpcServiceImpl(IotAlarmTriggerService iotAlarmTriggerService) {
        this.iotAlarmTriggerService = iotAlarmTriggerService;
    }

    @Override
    public IotAlarmTriggerDTO insert(IotAlarmTriggerDTO iotAlarmTriggerDTO) {
        try {
            return convert(iotAlarmTriggerService.insert(convert(iotAlarmTriggerDTO)));
        } catch (Exception e) {
            logger.error("IotAlarmTrigger create failed.", e);
            throw new RuntimeException("IotAlarmTrigger create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(IotAlarmTriggerDTO iotAlarmTriggerDTO) {
        try {
            return iotAlarmTriggerService.updateInfoById(convert(iotAlarmTriggerDTO));
        } catch (Exception e) {
            logger.error("IotAlarmTrigger update by id failed.", e);
            throw new RuntimeException("IotAlarmTrigger update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(IotAlarmTriggerDTOQuery iotAlarmTriggerDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (iotAlarmTriggerDTOQuery.getW_id() != null && iotAlarmTriggerDTOQuery.getW_id() > 0) {
                query.put("w_id", iotAlarmTriggerDTOQuery.getW_id());
            }

            if (iotAlarmTriggerDTOQuery.getIds() != null && !iotAlarmTriggerDTOQuery.getIds().equals("")) {
                query.put("ids", iotAlarmTriggerDTOQuery.getIds());
            }

            if (iotAlarmTriggerDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", iotAlarmTriggerDTOQuery.getStopFlag());
            }

            if (iotAlarmTriggerDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", iotAlarmTriggerDTOQuery.getDeleteFlag());
            }

            return iotAlarmTriggerService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("IotAlarmTrigger update by query failed.", e);
            throw new RuntimeException("IotAlarmTrigger update by query failed, Please order your code.", e);
        }
    }

    @Override
    public IotAlarmTriggerDTO selectById(Integer id) {
        try {
            return convert(iotAlarmTriggerService.selectById(id));
        } catch (Exception e) {
            logger.error("IotAlarmTrigger select by id failed.", e);
            throw new RuntimeException("IotAlarmTrigger select by id failed, Please order your code.", e);
        }
    }

    @Override
    public IotAlarmTriggerDTO selectObjByQuery(IotAlarmTriggerDTOQuery iotAlarmTriggerDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            return convert(iotAlarmTriggerService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("IotAlarmTrigger select obj by query failed.", e);
            throw new RuntimeException("IotAlarmTrigger select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<IotAlarmTriggerDTO> getListForPage(IotAlarmTriggerDTOQuery iotAlarmTriggerDTOQuery) {
        Query query = Query.page(iotAlarmTriggerDTOQuery.getPn(), iotAlarmTriggerDTOQuery.getPageSize());

        if (iotAlarmTriggerDTOQuery.getId() != null && iotAlarmTriggerDTOQuery.getId() > 0) {
            query.put("id", iotAlarmTriggerDTOQuery.getId());
        }

        if (iotAlarmTriggerDTOQuery.getIds() != null && !iotAlarmTriggerDTOQuery.getIds().equals("")) {
            query.put("ids", iotAlarmTriggerDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(iotAlarmTriggerDTOQuery.getLikeName())) {
            query.put("likeName", iotAlarmTriggerDTOQuery.getLikeName());
        }

        if (iotAlarmTriggerDTOQuery.getEquipId() != null) {
            query.put("equipId", iotAlarmTriggerDTOQuery.getEquipId());
        }

        if (iotAlarmTriggerDTOQuery.getEquipIds() != null && !iotAlarmTriggerDTOQuery.getEquipIds().equals("")) {
            query.put("equipIds", iotAlarmTriggerDTOQuery.getEquipIds());
        }

        if (iotAlarmTriggerDTOQuery.getEquipId() != null) {
            query.put("equipId", iotAlarmTriggerDTOQuery.getEquipId());
        }

        if (iotAlarmTriggerDTOQuery.getSolveId() != null) {
            query.put("solveId", iotAlarmTriggerDTOQuery.getSolveId());
        }

        if (iotAlarmTriggerDTOQuery.getAlreadySolve() != null) {
            query.put("alreadySolve", iotAlarmTriggerDTOQuery.getAlreadySolve());
        }

        if (StringUtils.isNotBlank(iotAlarmTriggerDTOQuery.getStartDate())) {
            query.put("startDate", iotAlarmTriggerDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(iotAlarmTriggerDTOQuery.getEndDate())) {
            query.put("endDate", iotAlarmTriggerDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(iotAlarmTriggerDTOQuery.getOrderBy())) {
            query.put("orderBy", iotAlarmTriggerDTOQuery.getOrderBy());
        }

        if (iotAlarmTriggerDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", iotAlarmTriggerDTOQuery.getStopFlag());
        }

        if (iotAlarmTriggerDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", iotAlarmTriggerDTOQuery.getDeleteFlag());
        }

        List<IotAlarmTriggerDTO> list = convert(iotAlarmTriggerService.selectByQuery(query));
        Page<IotAlarmTriggerDTO> page = Page.from(iotAlarmTriggerDTOQuery.getPn(), iotAlarmTriggerDTOQuery.getPageSize(), list);
        page.setTotalItems(iotAlarmTriggerService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return iotAlarmTriggerService.deleteById(id);
        } catch (Exception e) {
            logger.error("IotAlarmTrigger delete failed.", e);
            throw new RuntimeException("IotAlarmTrigger delete failed, Please order your code.", e);
        }
    }
}